﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Linq;
using System.Linq;
using System.IO;
using System.Diagnostics;

namespace net461
{
    public class XmlHelper
    {
        public static String Path
        {
            get
            {
                String path = String.Format("{0}\\QK365.Core.DB.xml", Environment.CurrentDirectory);
                return path;
            }
        }

        /// <summary>
        /// 通过文件读取xml
        /// </summary>
        public static void QueryElementByFile()
        {
            Stopwatch watch = Stopwatch.StartNew();
            XElement root = XElement.Load(Path);
            StringBuilder sb = new StringBuilder();
            string name = string.Empty;
            string chinese = string.Empty;
            string entityName = string.Empty;
            List<KeyValue> existPropertyList = new List<KeyValue>();
            int isPublic = 0;
            var memberList = root.Elements("members").Elements("member").ToList();
            Console.WriteLine("字段数量：" + memberList.Count());
            foreach (var it in memberList)
            {
                //初始化字段属性
                name = string.Empty;
                chinese = string.Empty;
                entityName = string.Empty;
                isPublic = 0;
                var attrs = it.Attributes().ToList();
                var memberNodeName = attrs[0].Value;
                var arr = memberNodeName.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
                chinese = it.Value.Trim();
                #region 过滤过长的中文
                if (chinese.Contains("时间戳"))
                    chinese = "时间戳";
                if (chinese.Contains("交付类型"))
                    chinese = "交付类型";
                #endregion
                int length = arr.Length;
                entityName = arr[length - 2];
                if (entityName == "BaseData")
                    isPublic = 1;
                name = arr.Last();
                if (string.IsNullOrWhiteSpace(chinese))
                    chinese = name;
                if (existPropertyList.FirstOrDefault(x => x.Key == name && x.Value == entityName) != null)
                    continue;
                sb.Append($" INSERT INTO [BusinessLogDict] VALUES ('{name}','{chinese}','{entityName}',{isPublic},'',NULL,0,GETDATE(),0,GETDATE(),0)  \r\n");
                existPropertyList.Add(new KeyValue() { Key = name, Value = entityName });
            }
            Console.WriteLine("去重后字段数量：" + existPropertyList.Count());
            WriteTXT(sb.ToString(), "DicSql.sql");
            watch.Stop();
            Console.WriteLine($"导出SQL完成，耗时：{watch.ElapsedMilliseconds}毫秒。");
        }

        public static string WriteTXT(string html, string file)
        {
            FileStream fileStream = new FileStream(Environment.CurrentDirectory + "\\" + file, FileMode.Append);
            StreamWriter streamWriter = new StreamWriter(fileStream, Encoding.Default);
            streamWriter.Write(html + "\r\n");
            streamWriter.Flush();
            streamWriter.Close();
            fileStream.Close();
            return "true";
        }
    }
    public class KeyValue
    {
        public string Key { get; set; }
        public string Value { get; set; }
    }
}
